/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland02.model.task.impl;

import dawnland02.data.map.model.MatrixDirection;
import dawnland02.model.action.controller.MoveActionController;
import dawnland02.model.entity.EntityInternal;
import dawnland02.model.task.BaseTask;
import dawnland02.model.task.GroupTask;
import dawnland02.model.task.TaskStatus;
import dawnland02.service.GeneralService;
import java.util.List;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public class MoveToTask extends GroupTask{
    
    private Integer destinationMatrixCellId;
    
    public MoveToTask(EntityInternal entity, Integer destinationMatrixCellId) {
        super("MoveToTask", entity);
        this.destinationMatrixCellId = destinationMatrixCellId;
        init();
    }

    @Override
    protected void init(){
        List<Integer> path = GeneralService.getClosestPathAsIds(entity, entity.getMatrixCell().getId(), destinationMatrixCellId);
        path.remove(0);
        Integer previousMatrixCellId = entity.getMatrixCell().getId();
        //MatrixDirection previousMatrixDirection = entity.getDirection();
        for(Integer adiacentMatrixCellId : path){
            GroupTask groupTask = new GroupTask(entity);

            MatrixDirection direction = GeneralService.getMatrix().getMatrixDirectionBetweenTwoNeighbours(previousMatrixCellId, adiacentMatrixCellId);
            RotateTask rotateTask = new RotateTask(entity, direction);
            groupTask.addTask(rotateTask);

            BaseTask moveTask = new BaseTask("MoveTask", entity);
            MoveActionController moveActionController = new MoveActionController(entity, moveTask, adiacentMatrixCellId);
            moveTask.addAction(moveActionController.getAction());
            groupTask.addTask(moveTask);

            this.addTask(groupTask);

            previousMatrixCellId = adiacentMatrixCellId;
        }
        RotateTask rotateTask = new RotateTask(entity, MatrixDirection.UP);
        addTask(rotateTask);

        setTaskStatus(TaskStatus.INITIALISED);
    }

    @Override
    protected void prepare() {
        //nothing more to be done
        setTaskStatus(TaskStatus.PREPARED);
    }

}
